package com.gxa.uhome.controller;

import com.gxa.uhome.exception.BusinessException;
import com.gxa.uhome.pojo.GoodsType;
import com.gxa.uhome.service.GoodsTypeService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: Sanruy Mu
 * Date: 2021/06/07 13:57
 * Description:
 * Version: V1.0
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/goodsType", method = RequestMethod.POST)
@Api(tags = "GoodsType商品品类管理模块", produces = "application/json;charset=UTF-8")
public class GoodsTypeController {
    @Resource
    GoodsTypeService goodsTypeService;

    @ApiOperation(value = "全查询", notes = "全查询商品品类", httpMethod = "POST")
    @PostMapping("/selectALl")
    public JsonResult selectAll() {
        JsonResult jsonResult = new JsonResult();
        try {
            List<GoodsType> goodsTypeList = goodsTypeService.selectAll();
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(goodsTypeList);
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询商品品类", notes = "分页查询商品品类", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "json格式的page参数",
                    paramType = "query"),
            @ApiImplicitParam(name = "size", value = "json格式的size参数",
                    paramType = "query")
    })
    @PostMapping("/selectByPage")
    public JsonResult selectByPage(@RequestParam(value = "page") Integer page,
                                   @RequestParam(value = "size") Integer size) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<GoodsType> goodsTypesParent = goodsTypeService.selectByPageParent(page, size);
            if (goodsTypesParent != null) {
                jsonResult.setCode("1");
                jsonResult.setMsg("查询成功");
                jsonResult.setData(goodsTypesParent);
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "GoodsType", value = "json格式的商品品类对象，由pid（父类的id）、name 、 sort（排序）组成 ",
                    paramType = "body"
            )
    })
    @ApiOperation(value = "添加商品品类", notes = "添加商品品类", httpMethod = "POST")
    @PostMapping("/addGoodsType")
    public JsonResult addGoodsType(@RequestBody GoodsType goodsType) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer add = goodsTypeService.add(goodsType);
            if (add != null) {
                jsonResult.setCode("1");
                jsonResult.setMsg("注册成功");
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "编辑商品品类", notes = "编辑商品品类", httpMethod = "POST")
    @ApiImplicitParam(name = "GoodsType", value = "json格式的商品品类对象，由pid（父类的id）、name 、 sort（排序）组成，id为必传项，其余根据需要进行填写（至少有一个） ",
            paramType = "body"
    )
    @PostMapping("/editGoodType")
    public JsonResult editGoodType(@RequestBody GoodsType goodsType) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer edit = goodsTypeService.edit(goodsType);
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "json格式的商品品类ID",
                    paramType = "query")
    })
    @ApiOperation(value = "删除商品品类", notes = "删除商品品类", httpMethod = "POST")
    @PostMapping("/deletGoodsType")
    public JsonResult deletGoodsType(@RequestParam(value = "id") Integer id) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer delete = goodsTypeService.delete(id);
            if (delete != 0) {
                Integer integer = goodsTypeService.deleteByPid(id);
                if (integer != 0) {
                    jsonResult.setCode("1");
                    jsonResult.setMsg("删除成功");
                } else {
                    jsonResult.setCode("1");
                    jsonResult.setMsg("父类删除成功，未查询到可删除的子类");
                }
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "json格式的商品品类姓名",
                    paramType = "query")
    })
    @ApiOperation(value = "根据名称模糊查询商品品类", notes = "根据名称模糊查询商品品类", httpMethod = "POST")
    @PostMapping("/selectByLikeName")
    public JsonResult selectByLikeName(@RequestParam(value = "name") String name) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<GoodsType> goodsTypes = goodsTypeService.selectByLikeName(name);
            List<GoodsType> plist = new ArrayList<GoodsType>();
            List<GoodsType> newplist = new ArrayList<GoodsType>();
            if (goodsTypes != null) {
                for (GoodsType goodsType : goodsTypes) {
                    if (goodsType.getPid() == 0) {
                        List<GoodsType> goodsTypes1 = goodsTypeService.selectByPageSon(goodsType.getId());
                        goodsType.setListGoodsType(goodsTypes1);
                        plist.add(goodsType);
                    } else {
                        Integer pid = goodsType.getPid();
                        GoodsType goodsType1 = goodsTypeService.selectById(pid);
                        List<GoodsType> goodsTypes1 = goodsTypeService.selectByPageSon(goodsType1.getId());
                        goodsType1.setListGoodsType(goodsTypes1);
                        plist.add(goodsType1);
                    }
                }
                List<GoodsType> finalList = null;
                for (GoodsType goodsType : plist) {
                    if (!newplist.contains(goodsType)) {
                        newplist.add(goodsType);
                    }
                    finalList = newplist.stream().sorted(Comparator.comparingInt(GoodsType::getSort)).collect(Collectors.toList());
                }
                jsonResult.setCode("1");
                jsonResult.setMsg("查询成功");
                jsonResult.setData(finalList);
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }
}